package com.leo.oplayer.filter.bak;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import com.leo.oplayer.util.Constancts;
import com.leo.oplayer.util.DateUtil;

public class PhoneStatReceiver extends BroadcastReceiver{
	
	private static final String TAG = "Leo---PhoneStatReceiver";
	
	private static MyPhoneStateListener phoneListener = new MyPhoneStateListener();
	
	private static boolean incomingFlag = false;
	
	private static String incoming_number = null;
	private static String personId = null;
	private static String startTime = null;
	private static String endTime = null;
	
	private boolean isRunning = false;
	
	class SelectActivity  extends Activity{
		protected void onCreate(Bundle savedInstanceState){
			 super.onCreate(savedInstanceState);
			 setContentView(android.R.layout.select_dialog_item);
		}
		
	}


	@Override
	public void onReceive(Context context, Intent intent) {
		
		if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
			
			incomingFlag = false;
			String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
			
			
			Log.i(TAG, "call OUT:"+phoneNumber);
			
//			phoneListener.setOutPhoneNumber(phoneNumber);			
//			TelephonyManager telephony = (TelephonyManager) context
//			.getSystemService(Context.TELEPHONY_SERVICE);
//	        telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE); 
			
		}else{
			
			//来电的处理
			TelephonyManager tm = 
			    (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
			//记录来电号码
			
			switch (tm.getCallState()) {
			case TelephonyManager.CALL_STATE_RINGING:
				incomingFlag = true;//标识当前是来电
				incoming_number = intent.getStringExtra("incoming_number");
				Log.i(TAG, "RINGING :"+ incoming_number);

				//查找联系人ID
				
				//
				Log.d("incomingFliter", "call in............");			
				
//				new AlertDialog.Builder(context)
//                .setTitle("select")
//                .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int which) {
//
//                        /* User clicked so do some stuff */
////                        String[] items = getResources().getStringArray(R.array.select_dialog_items);
////                        new AlertDialog.Builder(context)
////                                .setMessage("You selected: " + which + " , " + items[which])
////                                .show();
//                    }
//                })
//                .create();
				
				
							

			    
				break;
			case TelephonyManager.CALL_STATE_OFFHOOK:				
				if(incomingFlag){
//				if(incomingFlag && personId != null){
					Log.i(TAG, "incoming ACCEPT :"+ incoming_number);
					startTime = DateUtil.getCurrentTime(Constancts.TIME_FORMAT_SAVE);
					
					//同步统计数据到月统计表
					
					//同步通讯详情数据到通讯历史表
					
				}
				break;
			
			case TelephonyManager.CALL_STATE_IDLE:				
				if(incomingFlag){
					Log.i(TAG, "incoming IDLE");
					//更新历史表中的通话结束时间，通话时长等
					endTime = DateUtil.getCurrentTime(Constancts.TIME_FORMAT_SAVE);
				}
				
				
				break;
			} 

		}
	}

}
